package org.ndx.jsg.util;

import java.io.File;
import java.io.FileInputStream;

import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.ContentHandler;

/**
 * Utility class for loading various metada kinds
 * @author ndx
 *
 */
public class MetadataLoader {
	ContentHandler contenthandler = new BodyContentHandler();
	private Parser parser = new AutoDetectParser();

	/**
	 * Load metadata for the given file
	 * @param f
	 * @return
	 * @throws Exception
	 */
	public Metadata getMetadata(File f) throws Exception {
		FileInputStream is = null;
		try {
			is = new FileInputStream(f);
	
			Metadata metadata = new Metadata();
			metadata.set(Metadata.RESOURCE_NAME_KEY, f.getName());
			parser.parse(is, contenthandler, metadata);
			return metadata;
		} finally {
			if (is != null)
				is.close();
		}
	}

}
